package org.hxyjs.dao.impl;

import org.hxyjs.dao.OrderRecordsDao;
import org.hxyjs.entity.OrderRecords;
import org.hxyjs.mapper.OrderRecordsMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("orderRecordsDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class OrderRecordsDaoImpl implements OrderRecordsDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private OrderRecordsMapper orderRecordsMapper;
    public List<OrderRecords>  getAllOrderRecordss() throws Exception{
        return orderRecordsMapper.selectByExample(null);
    }

    @Override
    public Integer deleteOrderRecordsByUshOrno(String ushOrno)  throws Exception{
        return orderRecordsMapper.deleteByPrimaryKey(ushOrno);
    }

    @Override
    public Integer updateOrderRecordsByUshOrno(OrderRecords orderRecords)  throws Exception{
        return orderRecordsMapper.updateByPrimaryKey(orderRecords);
    }

    @Override
    public Integer addOrderRecords(OrderRecords orderRecords)  throws Exception{
        orderRecords.setUshOrno(initId());
        return orderRecordsMapper.insertSelective(orderRecords);
    }
    public String initId(){
        String maxUshOrno = orderRecordsMapper.getMaxUshOrno();
        return GenNumTools.initId("v",4,maxUshOrno);
    }
    @Override
    public List<OrderRecords> getOrderRecordsByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)orderRecordsMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return orderRecordsMapper.getOrderRecordsByPage(map);
    }
    @Override
    public String getMaxUshOrno(){
        return orderRecordsMapper.getMaxUshOrno();
    }
}
